//
//  DLIconChoseVC.m
//  AllPass
//
//  Created by apple on 2021/11/2.
//

#import "DLIconChoseVC.h"
#import "DLIconColCell.h"
#import "ImageFactory.h"
@interface DLIconChoseVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,retain)UICollectionView * myCol;
@property(nonatomic,retain)NSMutableArray * dataArr;
@end

@implementation DLIconChoseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [NSMutableArray new];
    [self createUI];
    [self getImages];
}
-(void)getImages{
    for (int i =0; i<69; i++) {
        UIImage *image = [[ImageFactory sharedInstance] imageForIndex:i];
        [self.dataArr addObject:image];
    }
    [self.myCol reloadData];
}
#pragma mark -collectionview 数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DLIconColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DLIconColCell" forIndexPath:indexPath];
    if (indexPath.row < self.dataArr.count) {
        cell.img = self.dataArr[indexPath.row];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.dataArr.count) {
        if (self.choseIconIdxBlock) {
            self.choseIconIdxBlock(indexPath.row);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)createUI{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item的行间距和列间距
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    // 设置item的大小
    CGFloat itemWid = floor((SCREEN_WIDTH - 7)/8.0);
    layout.itemSize = CGSizeMake(itemWid, itemWid);
    self.myCol = [[UICollectionView alloc]initWithFrame:Frame(0, kTopHeight , SCREEN_WIDTH, SCREEN_HEIGHT - kTopHeight -kTabbarHeight) collectionViewLayout:layout];
    self.myCol.delegate =self;
    self.myCol.dataSource = self;
    self.myCol.backgroundColor = UIColorWhite;
    [self.view addSubview:self.myCol];
    [self.myCol registerNib:[UINib nibWithNibName:@"DLIconColCell" bundle:nil] forCellWithReuseIdentifier:@"DLIconColCell"];
}


@end
